import jieba
import os
import re
import uuid

import matplotlib.pyplot as plt
from wordcloud import WordCloud


def creater(thestr, filepath, opt):
    filename = str(uuid.uuid1()) + '.png'
    print(os.getcwd())
    if opt == 1:
        # 中文词云生成
        pat = re.compile(r"[\u4e00-\u9fa5]")
        res = pat.findall(thestr)
        tem = ""
        data_str = tem.join(res)
        data_changed = jieba.cut(data_str, cut_all=True)
        tem = " "
        data_end = tem.join(data_changed)
        wd = WordCloud(font_path=os.getcwd()+os.sep+'app'+os.sep+'myAppPack'+os.sep+'msyh.ttc')
        result = wd.generate(data_end)
        result.scale = 4
        plt.figure()
        plt.imshow(result)
        plt.axis('off')
        plt.savefig(filepath + os.sep + filename, dpi=200)
        pass
    else:
        # 英文词云生成
        wd = WordCloud()
        result = wd.generate(thestr)
        result.scale = 4
        plt.figure()
        plt.imshow(result)
        plt.axis('off')
        plt.savefig(filepath + os.sep + filename, dpi=200)
        pass
    return filename
    pass
# 测试用例
# if __name__ == '__main__':
#     address = os.getcwd().split(os.sep)
#     print(address)
#     del address[-1]
#     address.append('static')
#     print(address)
#     address.append('app')
#     address.append('img')
#     address_str = str(os.sep).join(address)
#     print(address_str)
#     creater('a阿斯顿阿斯顿阿斯弗安法收到个法师带格式地方啊设定发安法啊是否安法啊地方啊设定发昂上的啊',opt=1)
